package com.cnhis.cloudhealth.clinical.clidoctor.clischemedefine.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clischemedefine.dao.CliSchemeDefineDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import java.util.HashMap;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

@Service
public class CliSchemeDefineBo
{
    @Resource
    private CliSchemeDefineDao cliSchemeDefineDao;
    
    @Resource
    private GetNewId getNewId;
    
    public ModelVo catalog()
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.catalog();
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo queryCatalogItem(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        Page ls = null;
        try
        {
            Object edate = map.get("edate");
            if (edate == null || "".equals(edate.toString()))
            {
                map.put("edate", DateUtils.getDateString(new Date()));
            }
            ls = cliSchemeDefineDao.queryCatalogItem(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls.getList());
        vo.setRows(ls.getList());
        return vo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo schemeDetail(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        Page ls = null;
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
            }
            ls = cliSchemeDefineDao.schemeDetail(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls.getList());
        vo.setRows(ls.getList());
        return vo;
    }
    
    public ModelVo allSearch(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        Page ls = null;
        try
        {
            ls = cliSchemeDefineDao.allSearch(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls.getList());
        vo.setRows(ls.getList());
        return vo;
    }
    
    public ModelVo queryOffice()
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.queryOffice();
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    public ModelVo schemeAlias(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.schemeAlias(map);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    public ModelVo adviceItemTree()
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.adviceItemTree();
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    public ModelVo getMedical(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        Page ls = null;
        try
        {
            ls = cliSchemeDefineDao.getMedical(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls.getList());
        vo.setRows(ls.getList());
        return vo;
    }
    
    private String getgparam60()
        throws Exception
    {
        return cliSchemeDefineDao.getgparam60();
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo catalogDetail(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        Page ls = null;
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
            }
            map.put("gparam60", getgparam60());
            ls = cliSchemeDefineDao.catalogDetail(map, currPage, pageSize);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls.getList());
        vo.setRows(ls.getList());
        return vo;
    }
    
    public ModelVo medicalPrompt(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.medicalPrompt(map);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    @SuppressWarnings("unchecked")
    private String getSysParamValue(Integer productId, Integer paramId, Integer paramNo)
        throws Exception
    {
        HashMap hs = new HashMap();
        hs.put("productId", productId);
        hs.put("programId", paramId);
        hs.put("paramNo", paramNo);
        return cliSchemeDefineDao.getSysParamValue(hs);
    }
    
    private List getTmpBCK(Map map, String aParamno)
        throws Exception
    {
        
        if ("3".equals(aParamno))
        {
            if ("1".equals(map.get("lACF01").toString()))
            {
                return cliSchemeDefineDao.getTmpBCK1(map);
            }
        }
        if ("0".equals(aParamno))
        {
            if ("1".equals(getSysParamValue(9999, 9999, 57)))
            {
                return cliSchemeDefineDao.getTmpBCK3(map);
            }
            else
            {
                return cliSchemeDefineDao.getTmpBCK4(map);
            }
        }
        return null;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo fetchTreatment(Map map)
        throws Exception
    {
        
        ModelVo vo = new ModelVo();
        List ls = null;
        List tempBCkList = null;
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
            }
            String aParamno = getSysParamValue(9999, 9999, 15);
            
            String para359_395 = "0";
            if ("1".equals(map.get("lACF01").toString()))
            {
                para359_395 = getSysParamValue(100, 105002, 359);
            }
            if ("2".equals(map.get("lACF01").toString()))
            {
                para359_395 = getSysParamValue(100, 105003, 395);
            }
            if (Integer.parseInt(map.get("lACF01").toString()) >= 1)
            {
                // 查询
                tempBCkList = getTmpBCK(map, aParamno);
            }
            else
            {
                aParamno = "1";
            }
            map.put("aParamno", aParamno);
            map.put("tempBCkList", tempBCkList);
            map.put("para359_395", para359_395);
            ls = cliSchemeDefineDao.fetchTreatment(map);
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
        
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo herbCommonUseList(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            Object lDate = map.get("lDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("lDate", DateUtils.getDateStr(new Date()) + " 23:59:59.999");
            }
            String aParamno = getSysParamValue(9999, 9999, 15);
            map.put("aParamno", aParamno);
            ls = cliSchemeDefineDao.herbCommonUseList(map);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo findSpecialTreament(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            Object lDate = map.get("aDate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("aDate", DateUtils.getDateString(new Date()));
            }
            
            ls = cliSchemeDefineDao.findSpecialTreament(map);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        return vo;
    }
    
    @SuppressWarnings("unchecked")
    public ModelVo getHerbById(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            Object lDate = map.get("adate");
            if (lDate == null || "".equals(lDate.toString()))
            {
                map.put("adate", DateUtils.getDateString(new Date()));
            }
            
            ls = cliSchemeDefineDao.getHerbById(map);
            
        }
        catch (Exception e)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            return vo;
        }
        vo.setResult(StaticKeys.OH_SUCCESS);
        vo.setList(ls);
        vo.setRows(ls);
        return vo;
    }
    
    public ModelVo checkCode(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo();
        if (cliSchemeDefineDao.checkCode(map) > 0)
        {
            vo.setResult(StaticKeys.OH_SUCCESS);
        }
        else
        {
            vo.setResult(StaticKeys.OH_FALSE);
        }
        return vo;
    }
    
    private void updateBBX(Map map)
        throws Exception
    {
        cliSchemeDefineDao.updateBBX(map);
    }
    
    private void deleteBBX(Map map)
        throws Exception
    {
        cliSchemeDefineDao.deleteBBX(map);
    }
    
    private Map getMaxAndMinID(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.getMaxAndMinID(map);
    }
    
    private Map getBCE02AndBCE03(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.getBCE02AndBCE03(map);
    }
    
    @SuppressWarnings("unchecked")
    private void insertBBX(Map map)
        throws Exception
    {
        cliSchemeDefineDao.insertBBX(map);
    }
    
    private void insertBDK(Map map)
        throws Exception
    {
        map.put("newID", getNewId.nextId());
        cliSchemeDefineDao.insertBDK(map);
    }
    
    private boolean existsBAR(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.existsBAR(map) > 0;
    }
    
    private void insertBAR(Map map)
        throws Exception
    {
        map.put("newID", getNewId.nextId());
        cliSchemeDefineDao.insertBAR(map);
    }
    
    private Map getMaxAndMin(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.getMaxAndMin(map);
    }
    
    private Map getParam(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.getParam(map);
    }
    
    private Integer getlVAF59(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.getlVAF59(map);
    }
    
    @SuppressWarnings("unchecked")
    private void insertBEF(Map map)
        throws Exception
    {
        Map m = getMaxAndMinID(map);
        Integer aMinsz = Integer.parseInt(m.get("aminsz").toString());
        Integer aMaxsz = Integer.parseInt(m.get("amaxsz").toString());
        Long lBEF01 = 0l;
        Long lBEF01A = 0l;
        Long lBEF01B = 0l;
        int lVAF32 = 0;
        int lVAF59 = 0;
        int lRownr = 0;
        String lBDA01 = "";
        Integer aMinfr = 0;
        Integer aMaxfr = 0;
        while (aMinsz <= aMaxsz)
        {
            map.put("aMinsz", aMinsz);
            lVAF59 = getlVAF59(map);
            map.put("lVAF59", lVAF59);
            m = getMaxAndMin(map);
            aMinfr = Integer.parseInt(m.get("aminfr").toString());
            aMaxfr = Integer.parseInt(m.get("amaxfr").toString());
            while (aMinfr <= aMaxfr)
            {
                map.put("aMinfr", aMinfr);
                m = getParam(map);
                lBDA01 = m.get("bda01").toString();
                lVAF32 = Integer.parseInt(m.get("vaf32").toString());
                lRownr = Integer.parseInt(m.get("rownr").toString());
                lBEF01 = getNewId.nextId();
                if ("1".equals(lBDA01) || "2".equals(lBDA01))
                {
                    if ((lVAF32 == 1) && (lRownr == 0))
                    {
                        lBEF01A = 0l;
                        lBEF01B = lBEF01;
                    }
                    else
                    {
                        // lBEF01A =lBEF01B;
                        lBEF01A = lBEF01;
                    }
                }
                else
                {
                    if (("8".equals(lBDA01)) || ("S".equals(lBDA01)) || ("E".equals(lBDA01)))
                    {
                        if (lRownr == 0)
                        {
                            lBEF01A = 0l;
                            lBEF01B = lBEF01;
                        }
                        else
                        {
                            // lBEF01A =lBEF01B;
                            lBEF01A = lBEF01;
                        }
                    }
                    else
                    {
                        lBEF01A = 0l;
                    }
                }
                // 增加方案明细
                map.put("lVAF59", lVAF59);
                map.put("lRownr", lRownr);
                map.put("lBEF01A", lBEF01A);
                map.put("lBEF01", lBEF01);
                insertBef1(map);
                aMinfr++;
            }
            
            aMinsz++;
        }
        
    }
    
    private void insertBef1(Map map)
        throws Exception
    {
        cliSchemeDefineDao.insertBef1(map);
    }
    
    // 成套方案新增
    @SuppressWarnings("unchecked")
    public ModelVo execClinicBEF1Insert(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
        String xml = map.get("lxml").toString();
        JSONObject obj = JSONObject.fromObject(xml);
        // 医疗项目
        Map TmpBBXMap = JsonUtil.parserToMap(obj.getJSONObject("BBX1"));
        // 方案明细
        List BEF1List = JsonUtil.parserToList(obj.getJSONArray("BEF1"));
        // 诊疗项目别名
        List BDK1List = JsonUtil.parserToList(obj.getJSONArray("BDK1"));
        // 方案应用
        List BAR1List = JsonUtil.parserToList(obj.getJSONArray("BAR1"));
        
        if (TmpBBXMap == null || TmpBBXMap.size() == 0)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            vo.setResultMsg("方案信息为空");
            return vo;
        }
        if (BEF1List == null || BEF1List.size() == 0)
        {
            vo.setResult(StaticKeys.OH_FALSE);
            vo.setResultMsg("方案明细信息为空");
            return vo;
        }
        map.put("TmpBBXMap", TmpBBXMap);
        map.put("BEF1List", BEF1List);
        map.put("BDK1Map", BDK1List);
        map.put("BAR1List", BAR1List);
        // 查询员工工号和姓名
        Map emp = getBCE02AndBCE03(map);
        map.put("lBCE02", emp.get("bce02"));
        map.put("lBCE03", emp.get("bce03"));
        
        // 修改
        if ("1".equals(map.get("lMode").toString()))
        {
            // 删除医疗项目
            deleteBBX(map);
            // 修改bbx1
            updateBBX(map);
        }
        else
        {
            // 新增bbx1
            insertBBX(map);
            
        }
        // 新增方案别名
        insertBDK(map);
        if (existsBAR(map))
        {
            // 新增诊疗应用
            insertBAR(map);
        }
        // 新增方案明细
        insertBEF(map);
        return vo;
    }
    
    private boolean existsBBX1(Map map)
        throws Exception
    {
        return cliSchemeDefineDao.existsBBX1(map) > 0;
    }
    
    private void updateBBX1(Map map)
        throws Exception
    {
        cliSchemeDefineDao.updateBBX1(map);
    }
    
    private void deleteBBX1(Map map)
        throws Exception
    {
        cliSchemeDefineDao.deleteBBX1(map);
    }
    
    // 删除成套定义方案
    public ModelVo execClinicBBXDEL(Map map)
        throws Exception
    {
        ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
        if (existsBBX1(map))
        {
            // 修改
            updateBBX1(map);
        }
        else
        {
            // 删除
            deleteBBX1(map);
        }
        return vo;
    }
    
    public ModelVo getVaf1Bybbx01a_8(HashMap map)
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.getVaf1Bybbx01a_8(map);
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setList(ls);
            return vo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            vo.setResultMsg(e.toString());
            return vo;
        }
        
    }

    public ModelVo adviceSousuo(HashMap map)
    {
        ModelVo vo = new ModelVo();
        List ls = new ArrayList<>();
        try
        {
            String aParamno  = getSysParamValue(9999, 9999, 15);
            String aParam1  = getSysParamValue(9999, 9999, 1);
            String aParam60  = getSysParamValue(9999, 9999, 60);
            String aParam106  = getSysParamValue(9999, 9999, 106);
            map.put("aParamno", aParamno);
            map.put("aParam1", aParam1);
            map.put("aParam60", aParam60);
            map.put("aParam106", aParam106);
            if ("1".equals(aParam106))
            {
                ls = cliSchemeDefineDao.adviceSousuo_kucui(map);
            }
            else
            {
                ls = cliSchemeDefineDao.adviceSousuo(map);
            }
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setList(ls);
            return vo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            vo.setResultMsg(e.toString());
            return vo;
        }
    }

    public ModelVo getVaf1Bybbx01a_s(HashMap map)
    {
        ModelVo vo = new ModelVo();
        List ls = null;
        try
        {
            ls = cliSchemeDefineDao.getVaf1Bybbx01a_s(map);
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setList(ls);
            return vo;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            vo.setResult(StaticKeys.OH_FALSE);
            vo.setResultMsg(e.toString());
            return vo;
        }
    }
}
